/////////////////////////////////////////////////////////////////////////
//
// Amuse Engine SDK - core\math\matrix
// Copyright (c) 2014.  All Rights Reserved
//
// File:		AEMatrix3f.cpp
// Author:		Gianluca Belardelli
// Date:		03/02/2014
//
/////////////////////////////////////////////////////////////////////////
#include <AECore.h>

void AEMatrix3f::SetScalingMatrix( AEVector4fRefParam vcScaleXYZ )
{
	m_vcColumn0.Set( vcScaleXYZ(0), 0.0f, 0.0f, 0.0f );
	m_vcColumn1.Set( 0.0f, vcScaleXYZ(1), 0.0f, 0.0f );
	m_vcColumn2.Set( 0.0f, 0.0f, vcScaleXYZ(2), 0.0f );
}

void AEMatrix3f::SetRotationMatrixX( AEFLOAT32 fAngle )
{
	const AEFLOAT32 fCosTheta = AEMath::Cos( fAngle );
	const AEFLOAT32 fSinTheta = AEMath::Sin( fAngle );

	m_vcColumn0.Set( 1.0f, 0.0f, 0.0f, 0.0f );
	m_vcColumn1.Set( 0.0f, fCosTheta, fSinTheta, 0.0f );
	m_vcColumn2.Set( 0.0f, -fSinTheta, fCosTheta, 0.0f );
}

void AEMatrix3f::SetRotationMatrixY( AEFLOAT32 fAngle )
{
	const AEFLOAT32 fCosTheta = AEMath::Cos( fAngle );
	const AEFLOAT32 fSinTheta = AEMath::Sin( fAngle );

	m_vcColumn0.Set( fCosTheta, 0.0f, -fSinTheta, 0.0f );
	m_vcColumn1.Set( 0.0f, 1.0f, 0.0f, 0.0f );
	m_vcColumn2.Set( fSinTheta, 0.0f, fCosTheta, 0.0f );
}

void AEMatrix3f::SetRotationMatrixZ( AEFLOAT32 fAngle )
{
	const AEFLOAT32 fCosTheta = AEMath::Cos( fAngle );
	const AEFLOAT32 fSinTheta = AEMath::Sin( fAngle );

	m_vcColumn0.Set( fCosTheta, fSinTheta, 0.0f, 0.0f );
	m_vcColumn1.Set( -fSinTheta, fCosTheta, 0.0f, 0.0f );
	m_vcColumn2.Set( 0.0f, 0.0f, 1.0f, 0.0f );
}

void AEMatrix3f::SetRotationMatrix( AEVector4fRefParam vcAxis, AEFLOAT32 fAngle )
{

}

const AESimdFloat32 AEMatrix3f::GetDeterminant( void ) const
{
	//     |a b c| |c0r0 c1r0 c2r0|
	// A = |d e f| |c0r1 c1r1 c2r1|
	//     |g h i| |c0r2 c1r2 c2r2|

	// |A| = a(ei - fh) - b(di - fg) + c(dh - eg)
	const AESimdFloat32 fResult = 
		( m_vcColumn0.GetComponent<0>() *
			( ( m_vcColumn1.GetComponent<1>() * m_vcColumn2.GetComponent<2>() ) -
			  ( m_vcColumn2.GetComponent<1>() * m_vcColumn1.GetComponent<2>() )
			)
		) -
		( m_vcColumn1.GetComponent<0>() *
			( ( m_vcColumn0.GetComponent<1>() * m_vcColumn2.GetComponent<2>() ) -
			  ( m_vcColumn2.GetComponent<1>() * m_vcColumn0.GetComponent<2>() )
			)
		) +
		( m_vcColumn2.GetComponent<0>() *
			( ( m_vcColumn0.GetComponent<1>() * m_vcColumn1.GetComponent<2>() ) -
			  ( m_vcColumn1.GetComponent<1>() * m_vcColumn0.GetComponent<2>() )
			)
		);

	return fResult;


}
